package com.visikard.ui.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.edmodo.cropper.CropImageView;
import com.visikard.application.Constant;
import com.visikard.application.VKRegApplication;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponBuilderActivityV3;
import com.visikard.utility.ScalingUtilities;
import com.visikard.utility.ScalingUtilities.ScalingLogic;
import com.visikard.utility.Utils;

public class CouponCameraPreviewFragment extends BaseFragment {

	public static final String CROPPED_BITMAP = "cropped_bitmap";
	private int mAspectRatioX = 100;
	private int mAspectRatioY = 50;

	private CouponBuilderActivityV3 me;

	private View mView;
	private CropImageView mImageView;
	private Button mButton_save, mButton_cancel;

	@Override
	public void setArguments(Bundle args) {
		// TODO Auto-generated method stub
		super.setArguments(args);

	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);

	}

	@Override
	public void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		me = (CouponBuilderActivityV3) getActivity();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		mView = inflater.inflate(R.layout.coupon_camera_preview, null);
		initControls(mView);
		return mView;
	}

	private void initControls(View view) {
		mImageView = (CropImageView) view.findViewById(R.id.imageView1);
		mImageView.setFixedAspectRatio(true);
		mImageView.setGuidelines(0);
		mImageView.setAspectRatio(mAspectRatioX, mAspectRatioY);

		mButton_save = (Button) view.findViewById(R.id.btn_save);
		mButton_cancel = (Button) view.findViewById(R.id.btn_cancel);
		mButton_save.setOnClickListener(new MyOnClickListener());
		mButton_cancel.setOnClickListener(new MyOnClickListener());
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			final String picturePath = bundle.getString("path");
			VKRegApplication.instance.putData("path", picturePath);
			final int[] dimen = VKRegApplication.instance.getDimensionDevice(me);
			int statusBarHeight = VKRegApplication.instance.getStatusBarHeight(me);
			cropButtonPressed(picturePath, dimen[0],
					(int) (dimen[1] - getResources().getDimension(R.dimen.dimen_150_300) - statusBarHeight));
		}
	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v == mButton_save) {

				try {
					Bitmap croppedImage = mImageView.getCroppedImage();
					if (croppedImage != null) {
						VKRegApplication.instance.putData(CROPPED_BITMAP, croppedImage);
						CouponInfoFragment couponInfoFragment = (CouponInfoFragment) getFragmentManager()
								.findFragmentByTag(Constant.COUPON_INFO_FRAGMENT_TAG);
						if (couponInfoFragment == null) {
							couponInfoFragment = new CouponInfoFragment();
						}

						Utils.toFragment(getFragmentManager(), couponInfoFragment, Constant.COUPON_INFO_FRAGMENT_TAG,
								R.id.framelayout_coupon_builder);
					}
					else {
						Utils.showToast(me, me.getString(R.string.unknown_problem), 0);
					}
				}
				catch (NullPointerException e) {
					// TODO: handle exception
				}

			}
			else if (v == mButton_cancel) {
				FragmentManager fm = getActivity().getSupportFragmentManager();
				fm.popBackStack();
			}
		}

	}

	protected void cropButtonPressed(String path, int mDstWidth, int mDstHeight) {

		// try {
		// Bitmap unscaledBitmap = ScalingUtilities.decodeResource(path, mDstWidth, mDstHeight, ScalingLogic.CROP);
		// if (unscaledBitmap != null) {
		// Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, mDstWidth, mDstHeight,
		// ScalingLogic.CROP);
		// if (scaledBitmap != null) {
		//
		// unscaledBitmap.recycle();
		// mImageView.setLayoutParams(new RelativeLayout.LayoutParams(mDstWidth, mDstHeight));
		// mImageView.setImageBitmap(scaledBitmap);
		// }
		// mImageView.setImageBitmap(scaledBitmap);
		// }
		//
		// }
		// catch (NullPointerException e) {
		// // TODO: handle exception
		// }
		try {
			Bitmap unscaledBitmap = ScalingUtilities.decodeResource(path, mDstWidth, mDstHeight, ScalingLogic.CROP);
			if (unscaledBitmap != null) {
				int degree = Utils.getOrientation(path);

				Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(Utils.setRotate(unscaledBitmap, degree),
						mDstWidth, mDstHeight, ScalingLogic.CROP);
				if (scaledBitmap != null) {

					unscaledBitmap.recycle();
					mImageView.setLayoutParams(new RelativeLayout.LayoutParams(mDstWidth, mDstHeight));
					mImageView.setImageBitmap(scaledBitmap);
				}
				mImageView.setImageBitmap(scaledBitmap);
			}

		}
		catch (Exception e) {
		}

	}

}
